package com.hz.sdka.sdka;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.anythink.rewardvideo.api.ATRewardVideoAd;
import com.anythink.rewardvideo.api.ATRewardVideoListener;
import com.hz.sdka.commona.api.reward.HZRewardVideo;
import com.hz.sdka.commona.api.reward.OnHZRewardVideoListener;
import com.hz.sdka.commona.base.BaseAd;
import com.hz.sdka.commona.bean.HZAdError;
import com.hz.sdka.commona.type.HZAdType;
import com.hz.sdka.commona.utils.LogUtils;

public class AdRewardVideoa extends BaseAd implements HZRewardVideo {

    private String TAG = "[rewardVideo] ===>";

    private ATRewardVideoAd mATRewardVideoAd;

    private OnHZRewardVideoListener mOnHZRewardVideoListener;

    public AdRewardVideoa(Context activity, String placeId) {
        super(placeId, HZAdType.REWARD_VIDEO.indexOf());
        mATRewardVideoAd = new ATRewardVideoAd(activity, placeId);
        mATRewardVideoAd.setAdListener(new ATRewardVideoListener() {

            @Override
            public void onRewardedVideoAdLoaded() {
                LogUtils.d(TAG, "[onRewardedVideoAdLoaded]");
                if (mOnHZRewardVideoListener != null) {
                    mOnHZRewardVideoListener.onRewardVideoLoaded();
                }
            }

            @Override
            public void onRewardedVideoAdFailed(AdError adError) {
                LogUtils.d(TAG, "[onRewardedVideoAdFailed] code:" + adError.getCode() + "  msg:" + adError.getDesc());
                if (mOnHZRewardVideoListener != null) {
                    mOnHZRewardVideoListener.onRewardVideoLoadFailed(new HZAdError(adError.getCode(),
                            adError.getDesc(),
                            adError.getPlatformCode(),
                            adError.getPlatformMSG()));
                }
            }

            @Override
            public void onRewardedVideoAdPlayStart(ATAdInfo atAdInfo) {
                LogUtils.d(TAG, "[onRewardedVideoAdPlayStart]");
                if (mOnHZRewardVideoListener != null) {
                    hzAdInfo.setEcpm(String.valueOf(atAdInfo.getEcpm()));
                    hzAdInfo.setNetworkPlacementId(atAdInfo.getNetworkPlacementId());
                    hzAdInfo.setNetworkType(atAdInfo.getAdNetworkType());
                    hzAdInfo.setShowId(atAdInfo.getShowId());
                    mOnHZRewardVideoListener.onRewardVideoShown(hzAdInfo);
                }
            }

            @Override
            public void onRewardedVideoAdPlayEnd(ATAdInfo atAdInfo) {
                LogUtils.d(TAG, "[onRewardedVideoAdPlayEnd]");
                if (mOnHZRewardVideoListener != null) {
                    mOnHZRewardVideoListener.onRewardVideoPlayComplete();
                }
            }

            @Override
            public void onRewardedVideoAdPlayFailed(AdError adError, ATAdInfo atAdInfo) {
                LogUtils.d(TAG, "[onRewardedVideoAdPlayFailed]");
                if (mOnHZRewardVideoListener != null) {
                    mOnHZRewardVideoListener.onRewardVideoPlayError();
                }
            }

            @Override
            public void onRewardedVideoAdClosed(ATAdInfo atAdInfo) {
                LogUtils.d(TAG, "[onRewardedVideoAdClosed]");
                if (mOnHZRewardVideoListener != null) {
                    mOnHZRewardVideoListener.onRewardVideoClosed();
                }
            }

            @Override
            public void onRewardedVideoAdPlayClicked(ATAdInfo atAdInfo) {
                LogUtils.d(TAG, "[onRewardedVideoAdPlayClicked]");
                if (mOnHZRewardVideoListener != null) {
                    mOnHZRewardVideoListener.onRewardVideoClicked();
                }
            }

            @Override
            public void onReward(ATAdInfo atAdInfo) {
                LogUtils.d(TAG, "[onReward]");
                if (mOnHZRewardVideoListener != null) {
                    mOnHZRewardVideoListener.onReward();
                }
            }
        });
    }


    @Override
    public void load() {
        if (mATRewardVideoAd == null) {
            if (mOnHZRewardVideoListener != null) {
                mOnHZRewardVideoListener.onRewardVideoLoadFailed(HZAdError.AD_OBJECT_IS_INVALID);
            }
        } else {
            mATRewardVideoAd.load();
        }
    }

    @Override
    public void setListener(OnHZRewardVideoListener listener) {
        this.mOnHZRewardVideoListener = listener;


    }

    @Override
    public void show(Activity mActivity, String scenarioId) {
        if (mActivity == null || mActivity.isDestroyed() || mActivity.isFinishing()) {
            if (mOnHZRewardVideoListener != null) {
                mOnHZRewardVideoListener.onRewardVideoShowFailed(HZAdError.ACTIVITY_IS_INVALID);
            }
            return;
        }
        if (!TextUtils.isEmpty(scenarioId)) {
            mATRewardVideoAd.show(mActivity, scenarioId);
        } else {
            mATRewardVideoAd.show(mActivity);
        }
    }

    /**
     * 获取广告价格
     */
    public double getEcpm() {
        if (mATRewardVideoAd != null && mATRewardVideoAd.isAdReady()) {
            return mATRewardVideoAd.checkAdStatus().getATTopAdInfo().getEcpm();
        }
        return 0.00;
    }


    public void createAd(Context activity, String placeId) {
        this.mATRewardVideoAd = new ATRewardVideoAd(activity, placeId);
//        String userid = "test_userid_001";
//        String userdata = "test_userdata_001";
//        Map<String, Object> localMap = new HashMap<>();
//        localMap.put(ATAdConst.KEY.USER_ID, userid);
//        localMap.put(ATAdConst.KEY.USER_CUSTOM_DATA, userdata);
//        localMap.put(ATAdConst.KEY.AD_ORIENTATION, ATAdConst.ORIENTATION_VERTICAL);
//        mATRewardVideoAd.setLocalExtra(localMap);
    }

    @Override
    public boolean isReady() {
        if (mATRewardVideoAd != null)
            return mATRewardVideoAd.isAdReady();
        else return false;
    }
}
